package com.example.bannerdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.VideoView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private ViewPager viewPager2;

    private ViewPager viewPager3;
    private VideoPagerAdapter adapter;
    private List<Integer> images; // 图片资源列表

    private VideoView videoView;
    private List<String> videoList;
    private final List<String> videoUrls = Arrays.asList(
            "http://vjs.zencdn.net/v/oceans.mp4",
            "https://media.w3.org/2010/05/sintel/trailer.mp4",
            "http://vjs.zencdn.net/v/oceans.mp4"
    );
    private int currentVideoIndex = 0;
    private int currentPage = 0;
    private VideoViewAdapter videoViewAdapter;
    private final Handler handler = new Handler();
    private final Runnable updatePageRunnable = new Runnable() {
        @Override
        public void run() {
            if (currentPage >= videoUrls.size()) {
                currentPage = 0;
            }
            viewPager3.setCurrentItem(currentPage++, true);
            handler.postDelayed(this, 30000); // 每隔 3 秒自动切换
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 设置适配器
        viewPager = findViewById(R.id.viewPager);
        images = Arrays.asList(R.drawable.image1, R.drawable.image2, R.drawable.image3); // 填充图片资源
        viewPager3 = findViewById(R.id.viewPager3);
        adapter = new VideoPagerAdapter(this, videoUrls);
        viewPager3.setAdapter(adapter);
        // 启动自动轮播
        handler.postDelayed(updatePageRunnable, 3000);

        // 处理页面切换，自动暂停和播放视频
        viewPager3.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {}


        });


        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return images.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setImageResource(images.get(position));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                container.addView(imageView);
                return imageView;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }
        });
        // 设置适配器
        viewPager2 = findViewById(R.id.viewPager2);

        // 初始化视频列表（这里假设视频放在raw目录下，或使用网络URL）
        videoList = new ArrayList<>();
        videoList.add("android.resource://" + getPackageName() + "/" + R.raw.video1);
        videoList.add("android.resource://" + getPackageName() + "/" + R.raw.video2);
        // 设置视频ViewPager的适配器
        videoViewAdapter = new VideoViewAdapter();
        viewPager2.setAdapter(videoViewAdapter);
        // 设置ViewPager的页面变化监听器，用于自动播放下一个视频
        viewPager2.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

            @Override
            public void onPageSelected(int position) {
                // 当页面被选中时，播放对应的视频
                playVideo(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {}
        });

        // 初始播放第一个视频
        playVideo(currentVideoIndex);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(updatePageRunnable);
    }
    private void playVideo(int position) {
        // 获取当前页面的VideoView，并播放对应位置的视频
        VideoView videoView = (VideoView) viewPager2.findViewWithTag(position);
        if (videoView != null) {
            videoView.setVideoURI(Uri.parse(videoList.get(position)));
            videoView.start();

            // 可以设置视频循环播放，但需要注意可能导致的内存和性能问题
            // videoView.setOnCompletionListener(mp -> videoView.start());
        }
    }
    private class VideoViewAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return videoList.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            VideoView videoView = new VideoView(MainActivity.this);
            videoView.setTag(position); // 为VideoView设置标签，以便后续查找
            container.addView(videoView);
            if(position == 0){
                videoView.post(()-> playVideo(position));
            }
            return videoView;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }
    }

}